package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        // 1. 从参数中获取用户名和密码
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        // 2. 根据用户名查询数据库中的数据
        Employee employee = employeeMapper.selectByUsername(username);

        // 3. 处理异常情况（用户名不存在）
        if (employee == null) {
            // 3.1. 账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 4. 处理异常情况（密码不对）
        // 4.1. md5 加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            // 4.2. 密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 5. 处理异常情况（账号被锁定）
        if (employee.getStatus() == StatusConstant.DISABLE) {
            // 5.1. 账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        // 6. 返回数据
        return employee;
    }

    /**
     * 新增员工
     *
     * @param employeeDTO
     * @return
     */
    @Override
    public void saveEmployee(EmployeeDTO employeeDTO) {
        // 1. 构建 Employee 实体对象
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        // 1.1. 设置账号状态，默认正常
        employee.setStatus(StatusConstant.ENABLE);
        // 1.2. 设置密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        // 2. 插入数据
        employeeMapper.insert(employee);
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // 1. 开启分页功能
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        // 2. 执行分页查询
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);

        // 3. 封装分页结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 启用禁用员工账号
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStopEmployee(Integer status, Long id) {
        // 1. 构造 Employee 实体对象
        Employee employee = Employee.builder()
                .id(id)
                .status(status)
                .build();

        // 2. 更新数据
        employeeMapper.update(employee);
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    @Override
    public Employee getById(Long id) {
        return employeeMapper.selectById(id);
    }

    /**
     * 更新员工信息
     *
     * @param employeeDTO
     */
    @Override
    public void updateEmployee(EmployeeDTO employeeDTO) {
        // 1. 构造 Employee 实体对象
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

        // 2. 更新数据
        employeeMapper.update(employee);
    }
}
